package com.sgb.member.ui.activity;

import android.app.Application;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.google.gson.Gson;
import com.sgb.member.MemberConfig;
import com.sgb.member.entity.ApplyCompanyBean;
import com.sgb.member.entity.BaseBean;
import com.sgb.member.entity.CompanyDeptBean;
import com.sgb.member.entity.MemberRelieveBean;
import com.sgb.member.entity.UserMsgEntity;
import com.sgb.member.service.MemberAPI;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.util.SharedPreferenceUtil;

import java.util.List;

import retrofit2.Call;

/**
 * 会员解除申请
 */
public class MemberRelieveModel extends AndroidViewModel {

    public MutableLiveData<Boolean> dialog = new MutableLiveData<>();
    public MutableLiveData<BaseBean> mRelieveSuccess = new MutableLiveData<>();
    public MutableLiveData<BaseEntity> isSuccessMsg = new MutableLiveData<>();
    public MutableLiveData<List<ApplyCompanyBean>> mCompanyList = new MutableLiveData<>();
    public MutableLiveData<List<CompanyDeptBean>> mDepList = new MutableLiveData<>();
    MemberAPI api;

    public MemberRelieveModel(@NonNull Application application) {
        super(application);
        api = MemberConfig.getInstance().getApi();
    }

    //flag 1 表示有审批 0 代表无审批  ，comType 表示是公司还是个人
//    public void memberReliveApply2(MemberRelieveBean bean, String flag, int comType) {
//        dialog.setValue(true);
//        if (comType == MemberConfig.WEBSITE_FOR_COMPANY) {
//            if (flag.equals("0")) { //无审批
//                if (bean.isPersonMember()) {   //个人
//                    api.personMemberRelieve2(bean).enqueue(new ICallBack<BaseEntity>() {
//                        @Override
//                        public void onSuccess(BaseEntity data) {
//                            dialog.setValue(false);
//                            if (data != null) {
//                                isSuccessMsg.postValue(data);
//                            }
//                        }
//
//                        @Override
//                        public void onFail() {
//                            dialog.setValue(false);
//                        }
//                    });
//                } else { //企业
//                    api.relieveMember2(bean).enqueue(new ICallBack<BaseEntity<BaseBean>>() {
//                        @Override
//                        public void onSuccess(BaseEntity<BaseBean> data) {
//                            dialog.setValue(false);
//                            if (data != null) {
//                                if (data.getState().equals("ok")) {
//                                    mRelieveSuccess.setValue(data.getData());
//                                }
//                            }
//                        }
//
//                        @Override
//                        public void onFail() {
//                            dialog.setValue(false);
//                        }
//                    });
//                }
//            } else {//有审批
//                if (bean.isPersonMember()) {
//                    api.relieveMemberApprove(bean).enqueue(new ICallBack<BaseEntity>() {
//                        @Override
//                        public void onSuccess(BaseEntity data) {
//                            dialog.setValue(false);
//                            if (data != null) {
//                                isSuccessMsg.postValue(data);
//                            }
//                        }
//
//                        @Override
//                        public void onFail() {
//                            dialog.setValue(false);
//                        }
//                    });
//                } else {//企业
//                    api.memberRelieveApply2(bean).enqueue(new ICallBack<BaseEntity>() {
//                        @Override
//                        public void onSuccess(BaseEntity data) {
//                            dialog.setValue(false);
//                            if (data != null) {
//                                if (data != null) {
//                                    isSuccessMsg.postValue(data);
//                                }
//                            }
//                        }
//
//                        @Override
//                        public void onFail() {
//                            dialog.setValue(false);
//                        }
//                    });
//                }
//            }
//        } else {
//            //个人没有审批的功能  跟企业解除 无审批 个人共用
//            api.personMemberRelieve2(bean).enqueue(new ICallBack<BaseEntity>() {
//                @Override
//                public void onSuccess(BaseEntity data) {
//                    dialog.setValue(false);
//                    if (data != null) {
//                        isSuccessMsg.postValue(data);
//                    }
//                }
//
//                @Override
//                public void onFail() {
//                    dialog.setValue(false);
//                }
//            });
//        }
//    }
    //解除会员
    public void delMember(MemberRelieveBean bean){
        MemberConfig.getInstance().getApi().delRelation(bean).enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
                dialog.setValue(false);
                if (data != null) {
                    isSuccessMsg.postValue(data);
                }
            }

            @Override
            public void onFail() {
                dialog.setValue(false);
            }
        });
    }

    // 审批【申请】企业列表
    public void getMyCompanyList() {
        dialog.setValue(true);
        api.getMyCompanyList().enqueue(new ICallBack<BaseEntity<List<ApplyCompanyBean>>>() {
            @Override
            public void onSuccess(BaseEntity<List<ApplyCompanyBean>> data) {
                mCompanyList.setValue(data.getData());
            }
        });
    }

    // 审批【申请】部门列表
    public void getDeptList(String companyNo) {
        dialog.setValue(true);
        api.getDeptList(companyNo).enqueue(new ICallBack<BaseEntity<List<CompanyDeptBean>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CompanyDeptBean>> data) {
                mDepList.setValue(data.getData());
            }
        });
    }

    public MutableLiveData<Boolean> userInfo = new MutableLiveData<>();

    public void getUserInfo() {
        dialog.setValue(true);
        api.getUserInfo().enqueue(new ICallBack<BaseEntity<UserMsgEntity>>() {
            @Override
            public void onSuccess(BaseEntity<UserMsgEntity> data) {
                SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.USER_NAME, data.getData().getUserName());
                SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.USER_NO, data.getData().getNo());
                SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.REAL_NAME, data.getData().getRealName());
                SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.DRIVER_AUTH, data.getData().getIsDriverAuth());
                SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.ENTERPRISE_AUTH, data.getData().getEnterpriseAuth());
                if (data.getData().getEnterprise() == null) {
                    userInfo.setValue(false);
                    return;
                }
                SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.COMPANY_NAME, data.getData().getEnterprise().getCompanyName());
                Log.e("公司no", data.getData().getEnterprise().getNo());
                SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.COMPANY_NO, data.getData().getEnterprise().getNo());
                SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.REAL_ID, data.getData().getEnterprise().getId());
                userInfo.setValue(true);
            }

            @Override
            public void onFailure(Call<BaseEntity<UserMsgEntity>> call, Throwable t) {
                super.onFailure(call, t);
                userInfo.setValue(false);
            }
        });
    }
}
